/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.panels;

import com.david.documentation.domain.entities.doku.Doku;
import com.david.documentation.service.category.CategoryService;
import com.david.documentation.service.search.SearchService;
import com.david.documentation.service.tag.TagService;
import com.david.documentation.web.view.components.AutocompleteDataBehavior;
import com.david.documentation.web.view.pages.SearchResultPage;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SearchPanel extends Panel{

	@SpringBean
	private SearchService searchService;

	@SpringBean
	private CategoryService categoryService;

	@SpringBean
	private TagService tagService;

	public SearchPanel(String id) {
		super(id);

		Form searchform = new Form("form", new Model<Doku>());

		final TextField<String> search = new TextField<String>("search",Model.of(""));
		final HiddenField<String> hidden = new HiddenField<String>("hidden",Model.of(""));
		search.add(new AutocompleteDataBehavior(hidden,searchService,categoryService,tagService));

		IndicatingAjaxButton submit = new IndicatingAjaxButton("submit") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (!StringUtils.isEmpty(search.getModelObject())){
					setResponsePage(SearchResultPage.class,
						new PageParameters("search="+search.getModelObject()+",type="+hidden.getModelObject()));
				}
			}
		};
		searchform.add(search);
		searchform.add(hidden);
		searchform.add(submit);
		add(searchform);
	}

}
